import { draw } from './tools.js'
import { APILogin } from './fetch.js'
import { clear } from './register.js'


let show_num = [];
//验证码渲染
draw(show_num)


//验证码切换
$('#canvas').click(function () {
    draw(show_num);
})

//输入判断
$('.layui-btn').on('click', function () {

    let username = $('.user .login-input').val();
    let pwd = $('.pwd .login-input').val();


    if (username == '') {
        layer.msg('请输入手机号');
        return;
    } else {
        if (pwd == '') {
            layer.msg('请输入密码');
            return;
        } else {
            let val = $(".input-val").val().toLowerCase();
            let num = show_num.join("");
            if (val == '') {
                layer.msg('请输入验证码');
                return;
            }
            if (val != num) {
                layer.msg('请输入正确的验证码');
                draw(show_num);
                return;
            }
            if (val == num) {
                //勾选框判断
                if ($('#check').prop('checked')) {

                    async function getCode() {
                        let { code, msg, user } = await APILogin({
                            mobile: username,
                            password: pwd,
                        })
                        if (code) {
                            layer.msg(msg);
                            //输入框清空
                            clear();
                            sessionStorage.setItem('login_user', user)
                            location.href = sessionStorage.loginBack ? sessionStorage.loginBack : '../../index.html'
                        } else {
                            layer.msg(msg);
                            draw(show_num);
                        }
                    }
                    getCode();
                } else {
                    layer.msg('您需要勾选同意条款才能使用', { icon: 5 });
                }
            }
        }
    }
}
)


//切换语言
$('.nav .zh-cn').on('click', function () {
    $('.user input').attr('placeholder', '请输入手机号');
    $('.pwd input').attr('placeholder', '请输入密码');
    $('.code input').attr('placeholder', '请输入验证码（不区分大小写）');
    $('.checkInfo').html(`我同意<span id="service">《服务条款》</span>和<span id="policy">《隐私政策》</span>`);
    $('.btn .layui-btn').html('立即登录');
    $('.reg').html(` <li><a href="../../html/register.html">立即注册</a> </li>|<li> <a href="../../html/userupdata.html">忘记密码？</a></li>`);
})

$('.nav .ZH-CN').on('click', function () {
    $('.user input').attr('placeholder', '請輸入手機號');
    $('.pwd input').attr('placeholder', '請輸入密碼');
    $('.code input').attr('placeholder', '請輸入驗證碼（不區分大小寫）');
    $('.checkInfo').html(`我同意<span id="service">《服務條款》</span>和<span id="policy">《隱私政策》</span>`);
    $('.btn .layui-btn').html('立即登錄');
    $('.reg').html(` <li><a href="../../html/register.html">立即註冊</a> </li>|<li> <a href="../../html/userupdata.html">忘記密碼？</a></li>`);
})

$('.nav .En').on('click', function () {
    $('.user input').attr('placeholder', 'Please enter your mobile phone number');
    $('.pwd input').attr('placeholder', 'Please enter Password');
    $('.code input').attr('placeholder', 'Please enter verification code (case insensitive)');
    $('.checkInfo').html(`I agree with the<span id="service">terms of service</span>and the<span id="policy">privacy policy</span>`);
    $('.btn .layui-btn').html('Sign in');
    $('.reg').html(` <li><a href="../../html/register.html">Register now</a> </li>|<li> <a href="../../html/userupdata.html">Forget the password?</a></li>`);
})


//layui服务条款
$('.msg #service').on('click', function () {
    layer.open({
        type: 2,
        area: ['700px', '450px'],
        fixed: false, //不固定
        maxmin: true,
        content: '../../html/service.html'
    });
})

$('.msg #policy').on('click', function () {
    layer.open({
        type: 2,
        area: ['700px', '450px'],
        fixed: false, //不固定
        maxmin: true,
        content: '../../html/policy.html'
    });
})


// $(document).off('click').on('click','###',function)